text = """Twinkle, twinkle, little star, 
How I wonder what you are! 
Up above the world so high, 
Like a diamond in the sky. 
Twinkle, twinkle, little star, 
How I wonder what you are! 
When the blazing sun is gone, 
When he nothing shines upon,
Then you show your little light, 
Twinkle, twinkle, all the night."""

# 统计歌词中每个英文单词出现数量

# 使用dict字典记录 key(词汇)/value(出现次数)
char_count = {}

# 转换为小写
text = text.lower()

# 替换','为''
# text = text.replace(',','')
# text = text.replace('.','')
# text = text.replace('!','')
# text = text.replace('\n','')

# str.maktrans() 创建转换映射
# translate 按照映射规则实现转换
trans = str.maketrans('','',',.!?\n')
text = text.translate(trans)

# print(text)
# 拆分文本
words = text.split(' ')

for word in words:
    # 字典.get() 方法检验key是否存在
    if char_count.get(word) is None:
        char_count[word] = 0
    char_count[word] += 1


# 打印字符数量
for key in char_count:
    print(key, ':', char_count[key])


# 这是一个拆分文本数单词数量的代码